/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifrn.Dao;

import br.edu.ifrn.Classes.Conexao;
import br.edu.ifrn.Classes.Turma;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Edmilson
 */
public class TurmaDao {
    private Connection connection;
    
    public TurmaDao(){
        this.connection = new Conexao().getConnection();
    }
    
    public void InserirTurma(Turma turma) {
        String sql = "insert into Turma" + "(id, nome, numeroSala, qtdAluno, idCurso)"
                + "values(?,?,?,?,?)";

        try {
            PreparedStatement stmt = connection.prepareStatement(sql);

            stmt.setInt(1, turma.getIdTurma());
            stmt.setString(2, turma.getNome());
            stmt.setInt(3, turma.getNumeroSala());
            stmt.setInt(4, turma.getQtdAlunos());
            stmt.setInt(5, turma.getIdCurso());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void removeTurma(Turma turma) {
        try {
            PreparedStatement stmt = connection.prepareStatement("delete from Turma where id=?");
            stmt.setInt(1, turma.getIdTurma());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    public void editarTurma(Turma turma){
        String sql = "UPDATE Turma SET nome=?, numeroSala=?, qtdAluno=? WHERE id=?";
        
        try{
            java.sql.PreparedStatement stmt = this.connection.prepareStatement(sql);
            stmt.setString(1, turma.getNome());
            stmt.setInt(2, turma.getNumeroSala());
            stmt.setInt(3, turma.getQtdAlunos());
            stmt.setInt(4, turma.getIdTurma());
            
            stmt.execute();
            stmt.close();
        }catch(SQLException e){
            throw new RuntimeException(e);            
        }
    }
    
    public List<Turma> getLista() {
        try {
            List<Turma> turmas = new ArrayList<Turma>();
            PreparedStatement stmt = this.connection.prepareStatement("select * from turma");
            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {
                // criando o objeto Turma
                Turma turma = new Turma();
                turma.setIdTurma(rs.getInt("id"));
                turma.setNome(rs.getString("nome"));
                turma.setNumeroSala(rs.getInt("numeroSala"));
                turma.setQtdAlunos(rs.getInt("qtdAluno"));

                // adicionando o objeto à lista
                turmas.add(turma);
            }
            rs.close();
            stmt.close();
            return turmas;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
